﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-05-17 14:16:41
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region 异常日志 数据访问层
	/// <summary>
	///异常日志 数据访问层
	/// </summary>	
	internal partial class LogError:ILogError
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.ConnectionEjautobaseKey;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(ErrorLogID),0)+1
                               FROM [LogError]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private LogErrorInfo GetByReader(IDataReader dr)
         {
            LogErrorInfo info = new LogErrorInfo();
            object obj;
            obj=dr["ErrorLogID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ErrorLogID = (long)obj;
            }
            obj=dr["PlatformID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.PlatformID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["RequestMethod"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.RequestMethod = obj.ToString();
            }
            obj=dr["RequestUrl"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.RequestUrl = obj.ToString();
            }
            obj=dr["APPVersion"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.APPVersion = obj.ToString();
            }
            obj=dr["PlatformVersion"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.PlatformVersion = obj.ToString();
            }
            obj=dr["UserID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UserID = (long)obj;
            }
            obj=dr["PostData"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.PostData = obj.ToString();
            }
            obj=dr["UA"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UA = obj.ToString();
            }
            obj=dr["Vendor"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Vendor = obj.ToString();
            }
            obj=dr["Model"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Model = obj.ToString();
            }
            obj=dr["Token"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Token = obj.ToString();
            }
            obj=dr["ClientId"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ClientId = obj.ToString();
            }
            obj=dr["Moble"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Moble = obj.ToString();
            }
            obj=dr["Imei"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Imei = obj.ToString();
            }
            obj=dr["Uuid"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Uuid = obj.ToString();
            }
            obj=dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedDate = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            obj=dr["ClientIP"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ClientIP = obj.ToString();
            }
            obj=dr["ErrorMessage"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ErrorMessage = obj.ToString();
            }
            obj=dr["ErrorSource"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ErrorSource = obj.ToString();
            }
            obj=dr["ErrorTrace"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ErrorTrace = obj.ToString();
            }
            return info;
         }
		public long Insert(LogErrorInfo info)
		{
        
        
            string strSql=@"INSERT INTO LogError			
			(			
			[PlatformID],
			[RequestMethod],
			[RequestUrl],
			[APPVersion],
			[PlatformVersion],
			[UserID],
			[PostData],
			[UA],
			[Vendor],
			[Model],
			[Token],
			[ClientId],
			[Moble],
			[Imei],
			[Uuid],
			[CreatedDate],
			[ClientIP],
			[ErrorMessage],
			[ErrorSource],
			[ErrorTrace]
			)
			 VALUES
			(			
			@PlatformID,
			@RequestMethod,
			@RequestUrl,
			@APPVersion,
			@PlatformVersion,
			@UserID,
			@PostData,
			@UA,
			@Vendor,
			@Model,
			@Token,
			@ClientId,
			@Moble,
			@Imei,
			@Uuid,
			@CreatedDate,
			@ClientIP,
			@ErrorMessage,
			@ErrorSource,
			@ErrorTrace
			)
			 SET @ErrorLogID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@ErrorLogID",DbType.Int64, sizeof(Int64));  
            db.AddInParameter(dbc, "@PlatformID",DbType.Int32, info.PlatformID);
            db.AddInParameter(dbc, "@RequestMethod",DbType.String, info.RequestMethod);
            db.AddInParameter(dbc, "@RequestUrl",DbType.String, info.RequestUrl);
            db.AddInParameter(dbc, "@APPVersion",DbType.String, info.APPVersion);
            db.AddInParameter(dbc, "@PlatformVersion",DbType.String, info.PlatformVersion);
            db.AddInParameter(dbc, "@UserID",DbType.Int64, info.UserID);
            db.AddInParameter(dbc, "@PostData",DbType.String, info.PostData);
            db.AddInParameter(dbc, "@UA",DbType.String, info.UA);
            db.AddInParameter(dbc, "@Vendor",DbType.String, info.Vendor);
            db.AddInParameter(dbc, "@Model",DbType.String, info.Model);
            db.AddInParameter(dbc, "@Token",DbType.String, info.Token);
            db.AddInParameter(dbc, "@ClientId",DbType.String, info.ClientId);
            db.AddInParameter(dbc, "@Moble",DbType.String, info.Moble);
            db.AddInParameter(dbc, "@Imei",DbType.String, info.Imei);
            db.AddInParameter(dbc, "@Uuid",DbType.String, info.Uuid);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@ClientIP",DbType.String, info.ClientIP);
            db.AddInParameter(dbc, "@ErrorMessage",DbType.String, info.ErrorMessage);
            db.AddInParameter(dbc, "@ErrorSource",DbType.String, info.ErrorSource);
            db.AddInParameter(dbc, "@ErrorTrace",DbType.String, info.ErrorTrace);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (long)db.GetParameterValue(dbc,"ErrorLogID");
         
		}
		
		
        public void Update(LogErrorInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE LogError SET ");
            sbSql.AppendLine("[PlatformID] = @PlatformID,");
                
            sbSql.AppendLine("[RequestMethod] = @RequestMethod,");
                
            sbSql.AppendLine("[RequestUrl] = @RequestUrl,");
                
            sbSql.AppendLine("[APPVersion] = @APPVersion,");
                
            sbSql.AppendLine("[PlatformVersion] = @PlatformVersion,");
                
            sbSql.AppendLine("[UserID] = @UserID,");
                
            sbSql.AppendLine("[PostData] = @PostData,");
                
            sbSql.AppendLine("[UA] = @UA,");
                
            sbSql.AppendLine("[Vendor] = @Vendor,");
                
            sbSql.AppendLine("[Model] = @Model,");
                
            sbSql.AppendLine("[Token] = @Token,");
                
            sbSql.AppendLine("[ClientId] = @ClientId,");
                
            sbSql.AppendLine("[Moble] = @Moble,");
                
            sbSql.AppendLine("[Imei] = @Imei,");
                
            sbSql.AppendLine("[Uuid] = @Uuid,");
                
            sbSql.AppendLine("[CreatedDate] = @CreatedDate,");
                
            sbSql.AppendLine("[ClientIP] = @ClientIP,");
                
            sbSql.AppendLine("[ErrorMessage] = @ErrorMessage,");
                
            sbSql.AppendLine("[ErrorSource] = @ErrorSource,");
                
            sbSql.AppendLine("[ErrorTrace] = @ErrorTrace");
                
            sbSql.Append(" where  [ErrorLogID] = @ErrorLogID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@ErrorLogID",DbType.Int64, info.ErrorLogID);
            db.AddInParameter(dbc, "@PlatformID",DbType.Int32, info.PlatformID);
            db.AddInParameter(dbc, "@RequestMethod",DbType.String, info.RequestMethod);
            db.AddInParameter(dbc, "@RequestUrl",DbType.String, info.RequestUrl);
            db.AddInParameter(dbc, "@APPVersion",DbType.String, info.APPVersion);
            db.AddInParameter(dbc, "@PlatformVersion",DbType.String, info.PlatformVersion);
            db.AddInParameter(dbc, "@UserID",DbType.Int64, info.UserID);
            db.AddInParameter(dbc, "@PostData",DbType.String, info.PostData);
            db.AddInParameter(dbc, "@UA",DbType.String, info.UA);
            db.AddInParameter(dbc, "@Vendor",DbType.String, info.Vendor);
            db.AddInParameter(dbc, "@Model",DbType.String, info.Model);
            db.AddInParameter(dbc, "@Token",DbType.String, info.Token);
            db.AddInParameter(dbc, "@ClientId",DbType.String, info.ClientId);
            db.AddInParameter(dbc, "@Moble",DbType.String, info.Moble);
            db.AddInParameter(dbc, "@Imei",DbType.String, info.Imei);
            db.AddInParameter(dbc, "@Uuid",DbType.String, info.Uuid);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@ClientIP",DbType.String, info.ClientIP);
            db.AddInParameter(dbc, "@ErrorMessage",DbType.String, info.ErrorMessage);
            db.AddInParameter(dbc, "@ErrorSource",DbType.String, info.ErrorSource);
            db.AddInParameter(dbc, "@ErrorTrace",DbType.String, info.ErrorTrace);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(long errorLogID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [LogError]  ");
            strSql.Append("WHERE [ErrorLogID]=@ErrorLogID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@ErrorLogID",DbType.Int64, errorLogID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<LogErrorInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<LogErrorInfo> list = new List<LogErrorInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "LogError");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    LogErrorInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public LogErrorInfo GetByID(long errorLogID)
		{
            Model.LogErrorInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [LogError] ");
            strSql.Append("WHERE [ErrorLogID]=@ErrorLogID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@ErrorLogID",DbType.Int64, errorLogID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}